package com.xhu.dao.impl;

import com.xhu.bean.pojo.SealControl;
import com.xhu.dao.SealControlDao;
import com.xhu.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class SealControlDaoImpl implements SealControlDao {


    @Override
    public boolean add(SealControl sealControl) {
        String sql = "insert into seal_control(title,type,state,date,controller) values(?,?,?,?,?)";
        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();
        int juge=0;
        try {
            //  数据库的名字 和 实体类必须保持一致
            juge = queryRunner.update(connection, sql, sealControl.getTitle(), sealControl.getType()
                    ,sealControl.getState(),sealControl.getDate(),sealControl.getController());
        } catch (SQLException ignored) {

        }finally {//最终都会被执行的代码
            ConnectionFactory.closeConnection(connection);
        }
        return juge == 1;
    }

    @Override
    public boolean update(SealControl sealControl) {
        String sql = "update manager set title=?,type=?,state=?,date=?,controller=? where id=?";
        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();
        int juge=0;
        try {
            //  数据库的名字 和 实体类必须保持一致
            juge = queryRunner.update(connection, sql, sealControl.getTitle(), sealControl.getType()
                    ,sealControl.getState(),sealControl.getDate(),sealControl.getController(),sealControl.getId());
        } catch (SQLException ignored) {

        }finally {//最终都会被执行的代码
            ConnectionFactory.closeConnection(connection);
        }
        return juge == 1;
    }

    @Override
    public boolean delete(SealControl sealControl) {
        String sql = "delete from manager where id=?";
        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();
        int juge=0;
        try {
            //数据库的名字 和 实体类必须保持一致
            juge = queryRunner.update(connection, sql, sealControl.getId());
        } catch (SQLException ignored) {

        }finally {//最终都会被执行的代码
            ConnectionFactory.closeConnection(connection);
        }
        return juge == 1;
    }

    @Override
    public List<SealControl> getAll() {
        String sql = "select * from manager";
        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();
        try {
            BeanListHandler<SealControl> handler=new BeanListHandler<>(SealControl.class);
            List<SealControl> list = queryRunner.query(connection,sql,handler);
            return list;
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }

    @Override
    public List<SealControl> getSome(java.sql.Date date) {
        String sql = "select * from manager where date=?";
        Connection connection= ConnectionFactory.openConnection();
        QueryRunner queryRunner=new QueryRunner();
        try {
            BeanListHandler<SealControl> handler=new BeanListHandler<>(SealControl.class);
            List<SealControl> list = queryRunner.query(connection,sql,handler,date);
            return list;
        }catch (Exception e){
            System.out.println(e);
        }
        return null;
    }
}
